package cn.lh.user.service.impl;

import cn.lh.basic.constants.VerifyCodeConstants;
import cn.lh.basic.service.IBaseService;
import cn.lh.basic.service.impl.BaseServiceImpl;
import cn.lh.basic.util.Md5Utils;
import cn.lh.basic.util.StrUtils;
import cn.lh.org.exception.BusinessException;
import cn.lh.user.domain.Logininfo;
import cn.lh.user.domain.User;
import cn.lh.user.dto.UserDto;
import cn.lh.user.mapper.LogininfoMapper;
import cn.lh.user.mapper.UserMapper;
import cn.lh.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public void phoneRegister(UserDto userDto) {
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        String pwd = userDto.getPwd();
        String confirmPwd = userDto.getConfirmPwd();

        if (StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(phoneCode) ||
                StringUtils.isEmpty(pwd) ||
                StringUtils.isEmpty(confirmPwd)
        ) {
            throw new BusinessException("参数不能为空");
        }
        if (!pwd.equals(confirmPwd)) {
            throw new BusinessException("两次密码输入不对应");
        }
        if (userMapper.findByPhone(phone) != null) {
            throw new BusinessException("该电话号码已被注册");
        }
        // 取出短信验证码和用户输入验证码进行校验
        Object codeObj = redisTemplate.opsForValue().get(VerifyCodeConstants.REGISTER_CODE_PREFIX + phone);
        if (codeObj == null) {
            throw new BusinessException("验证码过期");
        }
        String redisSmsCode = codeObj.toString().split(":")[1];
        if (!phoneCode.equals(redisSmsCode)) {
            throw new BusinessException("短信验证码不正确");
        }
        // 将userDto转为user
        User user = userDtoToUser(userDto);
        // 将user信息转为logininfo
        Logininfo logininfo = userToLogininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);

    }

    private User userDtoToUser(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // md5对盐值+密码进行加密
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        // 盐值+密码进行密码加密
        String md5Pwd = Md5Utils.encrypByMd5(salt + userDto.getPwd());
        user.setPassword(md5Pwd);
        return user;
    }

    private Logininfo userToLogininfo(User user) {
        Logininfo logininfo = new Logininfo();
        // 复制属性
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }
}
